package com.hopital5.zhangyz.service;

import com.hopital5.zhangyz.model.ActivityItem;
import com.hopital5.zhangyz.repository.ActivityItemRepository;
import com.hopital5.zhangyz.vo.request.ActivityItemVoRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ActivityItemService {
    @Autowired
    ActivityItemRepository activityItemRepository;

    public ActivityItem save(ActivityItemVoRequest activityItemVoRequest) {
        ActivityItem activityItem = ActivityItem.builder()
                .name(activityItemVoRequest.getName())
                .unit(activityItemVoRequest.getUnit())
                .build();
        return activityItemRepository.save(activityItem);
    }

    @Transactional
    public ActivityItem update(ActivityItemVoRequest activityItemVoRequest) {
        ActivityItem activityItem = activityItemRepository.findOne(activityItemVoRequest.getActivityItemId());
        activityItem.setName(activityItemVoRequest.getName());
        activityItem.setUnit(activityItemVoRequest.getUnit());
        return activityItem;
    }

    public ActivityItem findOne(Long id) {
        return activityItemRepository.findOne(id);
    }

    public void delete(Long id) {
        activityItemRepository.delete(id);
    }

    public void delete(ActivityItem activityItem) {
        activityItemRepository.delete(activityItem);
    }
}

